//1. 引入redux,
//2. createStore( reducer )
import {
  applyMiddleware,
  combineReducers,
  legacy_createStore as createStore,
  compose,
} from "redux";
import CityReducer from "./reducers/CityReducer";
import TabbarReducer from "./reducers/TabbarReducer";
import CinemasListReducer from "./reducers/CinemasListReducer";
import reduxThunk from "redux-thunk";
import reduxPromise from "redux-promise";

import { persistStore, persistReducer } from "redux-persist";
import storage from "redux-persist/lib/storage"; // defaults to localStorage for web
//持久化配置
const persistConfig = {
  key: "Joe-Darcy",
  storage,
  blacklist: ["CinemasListReducer"], //黑白名单
  whitelist: ["CityReducer"],
};

const reducer = combineReducers({
  CityReducer,
  TabbarReducer,
  CinemasListReducer,
});
//将reducer包裹持久化
const persistedReducer = persistReducer(persistConfig, reducer);

const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
// 创建的时候就要把中间件加上去   applyMiddleware应用中间件
//将store里的reducer换成持久化包裹的reducer
const store = createStore(
  persistedReducer,
  composeEnhancers(applyMiddleware(reduxThunk, reduxPromise))
);
let persistor = persistStore(store);
//持久化包裹store
export { store, persistor };

/*
  store.dispatch
  store.subscrbe
  store.getState
 */

//  function createKerwinStore(reducer){
//    var list = []
//    var state = reducer(undefined,{})/*调用是因为reducer初次返回一个默认值*/
//    function subscribe(callback){
/*subscrbe就是把回调函数传进来*/
//       list.push(callback)
//    }

//    function dispatch(action){
// dispatch把之前的state覆盖掉
//       state = reducer(state,action)
//       for(var i in  list){
//          list[i] && list[i]()
/*遍历list并且进行调用 */
//       }
//    }

//    function getState(){
// 返回最新的state值
//       return state
//    }
//    return {
//       subscribe,
//       dispatch,
//       getState
//    }
//  }

export default store;

/*
   var obj = {
     myname:"kerwin"
   }
   function test(obj2){
     var newobj = {...obj2}
     newobj.myname="xiaoming"
   
     return newobj
    }
    test(obj)


    纯函数： 
      1. 对外界没有副作用
      2. 同样的输入得到同样的输出
 */
